<template>
 <div class="rigister">
      <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 添加按钮 -->
    <el-button plain type="success" class="addbtn">添加角色</el-button>

    <!-- 表格 -->
    <el-table :data='rolesList'>
      <el-table-column  type="expand" >
        <template v-slot:default='{row}'>
          <p v-if='row.children.length ===0'>暂无权限</p>
          <el-row class="l1" v-for='l1 in row.children' :key='l1.id'>
            <el-col :span='4'>
              <!-- 一级 -->
              <el-tag @close='delright(rows,l1.id)' closable >{{l1.authName}}</el-tag>
              <i class="el-icon-arrow-right"></i>
            </el-col>
            <el-col :span='20' >
              <!-- 二级 -->
              <el-row class="l2" v-for='l2 in l1.children' :key=l2.id>
                <el-col :span='4'>
                  <el-tag @close='delright(row,l2.id)' closable  type='success'> {{ l2.authName }} </el-tag>
                  <i class="el-icon-arrow-right"></i>
                </el-col>
                <el-col :span='20'>
                  <!--三级 -->
                  <el-tag @close='delright(row,l3.id)' class="l3" closable type='warning' v-for='l3 in l2.children' :key=l3.id>{{ l3.authName }}</el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="#" type='index'></el-table-column>
      <el-table-column label="角色名称" prop='roleName'></el-table-column>
      <el-table-column label="描述" prop='roleDesc'></el-table-column>
      <el-table-column label="操作">
        <template v-slot:default='{ row }'>
          <el-button size="small" icon='el-icon-edit' plain type='primary'></el-button>
          <el-button size="small" icon='el-icon-delete' plain type='danger'></el-button>
          <el-button @click='showAssignDialog(row)' size="small" icon='el-icon-check' plain type='success'>分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分配对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="assignVisible"
      width="40%">
      <!-- 匿名插槽 -->
      <el-tree
      ref='tree'
      node-key='id'
      show-checkbox
      default-expand-all
      :data="data"
      :props="defaultProps" ></el-tree>

      <template v-slot:footer>
        <span  class="dialog-footer">
        <el-button @click="assignVisible = false">取 消</el-button>
        <el-button @click='assignRights' type="primary" >分配</el-button>
      </span>
      </template>
    </el-dialog>
 </div>
</template>

<script>
export default {
  data () {
    return {
      roleId: '',
      rolesList: [],
      assignVisible: false,
      data: [],
      defaultProps: {
        children: 'children', // 配置展开子集
        label: 'authName'// 配置展开文字
      }
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    async getRoleList () {
      const { meta, data } = await this.$axios.get('roles')
      if (meta.status === 200) {
        this.rolesList = data
      } else {
        this.$message.error(meta.msg)
      }
    },
    // 删除二级和三级权限
    async  delright (row, rightid) {
      const { meta, data } = await this.$axios.delete(`roles/${row.id}/rights/${rightid}`)
      // data是删除后剩余在权限
      // console.log(data)
      if (meta.status === 200) {
        this.$message.success(meta.msg)
        // this.getRoleList()
        row.children = data
      } else {
        this.$message.error(meta.msg)
      }
    },
    async showAssignDialog (row) {
      this.roleId = row.id
      this.assignVisible = true
      // 展开就发送Ajax请求获取全部权限
      const { meta, data } = await this.$axios.get(`rights/tree`)
      if (meta.status === 200) {
        this.data = data
        // 回显功能权限，选择一级后面回都被选择
        const ids = []
        row.children.forEach(l1 => {
          l1.children.forEach(l2 => {
            l2.children.forEach(l3 => {
              ids.push(l3.id)
            })
          })
        })
        this.$refs.tree.setCheckedKeys(ids)
      } else {
        this.$message.error(meta.msg)
      }
    },
    async assignRights () {
      const ids = this.$refs.tree.getCheckedKeys()// 获取半选择
      const halfs = this.$refs.tree.getHalfCheckedKeys()// 获取全选择
      const rids = [...ids, ...halfs].join(',')// 所有的

      // 发请求
      const { meta } = await this.$axios.post(`roles/${this.roleId}/rights`, { rids })

      if (meta.status === 200) {
        // 提示成功
        this.$message.success(meta.msg)
        // 关闭对话框
        this.assignVisible = false
        // 重新渲染
        this.getRoleList()
      } else {
        this.$message.error(meta.msg)
      }
    }
  }
}
</script>

<style lang='less' scoped>
   .addbtn{
     margin-bottom: 10px;
   }
   .l1{
     margin-bottom: 10px;
     border-bottom: 1px dotted #ccc;
   }
   .l2{
     margin-bottom: 10px;
   }
   .l3{
     margin-right: 5px;
     margin-bottom: 5px;
   }
</style>
